package com.ruoyi.marketers.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.marketers.domain.MarketersShop;
import com.ruoyi.marketers.mapper.MarketersApplyMapper;
import com.ruoyi.marketers.domain.MarketersApply;
import com.ruoyi.marketers.service.IMarketersApplyService;

/**
 * 商家审核Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-21
 */
@Service
public class MarketersApplyServiceImpl implements IMarketersApplyService 
{
    @Autowired
    private MarketersApplyMapper marketersApplyMapper;

    /**
     * 查询商家审核
     * 
     * @param applyId 商家审核主键
     * @return 商家审核
     */
    @Override
    public MarketersApply selectMarketersApplyByApplyId(Long applyId)
    {
        return marketersApplyMapper.selectMarketersApplyByApplyId(applyId);
    }

    /**
     * 查询商家审核列表
     * 
     * @param marketersApply 商家审核
     * @return 商家审核
     */
    @Override
    public List<MarketersApply> selectMarketersApplyList(MarketersApply marketersApply)
    {
        return marketersApplyMapper.selectMarketersApplyList(marketersApply);
    }

    /**
     * 新增商家审核
     * 
     * @param marketersApply 商家审核
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMarketersApply(MarketersApply marketersApply)
    {
        int rows = marketersApplyMapper.insertMarketersApply(marketersApply);
        insertMarketersShop(marketersApply);
        return rows;
    }

    /**
     * 修改商家审核
     * 
     * @param marketersApply 商家审核
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMarketersApply(MarketersApply marketersApply)
    {
        marketersApplyMapper.deleteMarketersShopByApplyId(marketersApply.getApplyId());
        insertMarketersShop(marketersApply);
        return marketersApplyMapper.updateMarketersApply(marketersApply);
    }

    /**
     * 批量删除商家审核
     * 
     * @param applyIds 需要删除的商家审核主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMarketersApplyByApplyIds(Long[] applyIds)
    {
        marketersApplyMapper.deleteMarketersShopByApplyIds(applyIds);
        return marketersApplyMapper.deleteMarketersApplyByApplyIds(applyIds);
    }

    /**
     * 删除商家审核信息
     * 
     * @param applyId 商家审核主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMarketersApplyByApplyId(Long applyId)
    {
        marketersApplyMapper.deleteMarketersShopByApplyId(applyId);
        return marketersApplyMapper.deleteMarketersApplyByApplyId(applyId);
    }

    @Override
    public List<MarketersApply> selectShopList(MarketersApply marketersApply) {
        return marketersApplyMapper.selectMarketersApplyList(marketersApply);
    }

    @Override
    public MarketersShop selectShopByShopsId(Long shopsId) {
        return marketersApplyMapper.selectShopByShopsId(shopsId);
    }

    @Override
    public int deleteShopsApplyByShopsId(Long shopsId) {
        return marketersApplyMapper.deleteShopsApplyByShopsId(shopsId);
    }

    @Override
    public int insertShop(MarketersShop marketersApply) {
        return marketersApplyMapper.insertMarketersShop(marketersApply);
    }

    /**
     * 新增商家店铺信息
     * 
     * @param marketersApply 商家审核对象
     */
    public void insertMarketersShop(MarketersApply marketersApply)
    {
        List<MarketersShop> marketersShopList = marketersApply.getMarketersShopList();
        Long applyId = marketersApply.getApplyId();
        if (StringUtils.isNotNull(marketersShopList))
        {
            List<MarketersShop> list = new ArrayList<MarketersShop>();
            for (MarketersShop marketersShop : marketersShopList)
            {
                marketersShop.setApplyId(applyId);
                list.add(marketersShop);
            }
            if (list.size() > 0)
            {
                marketersApplyMapper.batchMarketersShop(list);
            }
        }
    }
}
